<?php


function getDefaultGraphSettings() {
	$settings = new stdClass();
	$settings->axis = "left";
	$settings->title = "";
	$settings->hidden = "false";
	$settings->connect = "true";
	//$settings->fill_alpha = "10";
	
	return $settings;
}

function createValue($index,$value,$description ='') {
	if(!empty($description)) {
		$description = " description='$milestone'";
	}
	return "<value xid='$index'$description>$value</value>";
}

function GenerateChart($title,$seriesdata,$data,$settings ='',$useDefaultVisibility=false,$gadget = true,$htmlwrap=false) {
	if(empty($settings)) {
		if(is_array($data)) {
			foreach($data as $key => $value) {
				$settings[$key] = getDefaultGraphSettings();
			}
		} else {
			$settings = getDefaultGraphSettings();
		}
	}
	$gSettings = array();
	$xml = "" .
			"<chart>" .
			"<series>$seriesdata</series>" .
			"<graphs>";
	
	if(is_array($data)) {
		$i=1;
		foreach($data as $key => $value) {
			if(!empty($value)) {
				$xml .= "<graph gid='$i'>$value</graph>";
				
				$gSettings[$key] = "<graph gid='$i'>";
				if(is_array($settings)) {
					$stng = $settings[$key];
					
					if($i & 1) { // it's odd
						if(!$useDefaultVisibility) {
							$stng->hidden = "true";
						}
					}
					
					if(empty($stng->title)) {
						$stng->title = $key;
					}
					
					foreach((array) $stng as $k => $val) {
						$gSettings[$key] .= "<$k>$val</$k>";
					}
				} else {
					foreach((array) $settings as $k => $val) {
						$gSettings[$key] .= "<$k>$val</$k>";
					}
				}
				$gSettings[$key] .= "</graph>";
			}
			$i = $i+1;
		}
		
	} else {
		$xml .= "<graph gid='1'>$data</graph>";
		$setng = "";
		
		if(empty($settings->title)) {
			$settings->title = $title;
		}
		
		foreach((array) $settings as $k => $val) {
					$setng .= "<$k>$val</$k>";
		}
		
		$gSettings[] = "<graph gid='1'>$setng</graph>";
	}
	
	
	
	$xml .=	"</graphs>" .
			"</chart>";

	$settings = "<settings>" .
			"<decimals_separator>.</decimals_separator>" .
			"<thousands_separator>,</thousands_separator>" .
			"<digits_after_decimal>2</digits_after_decimal>" .
			"<legend><values><enabled>true</enabled><width>44</width><align>left</align><text>" . '{value}' . "</text></values></legend>";
	
	if($htmlwrap) {
		$settings .= "<labels><label><y>20</y><width>520</width><align>center</align><text>$title</text></label></labels>";
	}
	
	$settings .= "<graphs>";

	foreach($gSettings as $key => $value) {
		$settings .= $value;
	}
	
	$settings .= "</graphs></settings>";

	$chart = GenerateChartObject($xml,$settings,$gadget);

	if($htmlwrap == 'true' and !$gadget) {
		$chart = "<html><head><meta http-equiv=\"Content-Type\" content=\"text/html; charset=iso-8859-1\" />" .
		"<title>$title</title></head><body>$chart</body></html>";
		return $chart;
	} else {
		return $chart;
	}
	
}

function generateGraphSettings($title,$graphsettings,$gadget=false) {

		$settings = "<settings><decimals_separator>.</decimals_separator><thousands_separator>,</thousands_separator>" .
			"<legend><values><enabled>true</enabled><width>44</width><align>left</align><text>" . '{value}' . "</text></values></legend>";
			if(!$gadget) {
				$settings .= "<labels><label><y>20</y><width>520</width><align>center</align><text>$title</text></label></labels>";
			
			}
			$settings .= "<graphs>";
			if(is_array($graphsettings)) {
				foreach($graphsettings as $index => $s) {
					$settings .= $s;
				}
			}
			
			$settings .= "</graphs></settings>";
		
	return $settings;
}

function GenerateChartObject($data='',$settings='',$gadget=false) {
	global $CFG;
	
	$feedkey = optional_param('feedkey');
	if(!empty($feedkey)) {
		$feedkey = "&feedkey=$feedkey";
	}
	
	return InsertChart ( "{$CFG->wwwroot}/mercurius/amCharts/amline/amline.swf", "{$CFG->wwwroot}/mercurius/amCharts/amline/",$data,$settings,$gadget);
}

//====================================
function InsertChart( $flash_file, $path, $data='',$settings='', $gadget = false,$width="99%", $height="99%", $bg_color="FFFFFF", $transparent=false, $license=null ){
	$data=htmlentities($data, ENT_QUOTES);
	$settings=htmlentities($settings, ENT_QUOTES);
	$path=urlencode($path);
	$preloader_color = "#999999";
	
	if($gadget) {
		$obj = new stdClass();
		$obj->chart_data = $data;
		$obj->chart_settings = $settings;
		$obj->path = $path;
		$obj->preloader_color= $preloader_color;
		$obj->flash_file = $flash_file;
		return $obj;
	}
	
	$variables = "path=$path&preloader_color=$preloader_color&chart_settings=$settings&chart_data=$data";



	$html="<OBJECT classid='clsid:D27CDB6E-AE6D-11cf-96B8-444553540000' codebase='http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=8,0,0,0' ";
	
	$html.="WIDTH='".$width."' HEIGHT='".$height."' id='charts' ALIGN=''>\n";
	
	$u=(strpos ($flash_file,"?")===false)? "?" : ((substr($flash_file, -1)==="&")? "":"&");
	
	$html.="<PARAM NAME=movie VALUE='".$flash_file.$u.$variables;
	
	if($license!=null){$html.="&license=".$license;}
	
	$html.="'>\n<PARAM NAME=quality VALUE=high> <PARAM NAME=bgcolor VALUE=#".$bg_color.">\n";
	
	if($transparent){$html.="<PARAM NAME=wmode VALUE=transparent>\n";}
	
	$html.="<EMBED src='".$flash_file.$u.$variables;
	
	if($license!=null){$html.="&license=".$license;}
	
	$html.="' quality=high bgcolor=#".$bg_color." WIDTH='".$width."' HEIGHT='".$height."' NAME='charts' ALIGN='' swLiveConnect='true' ";
	
	if($transparent){$html.="wmode=transparent ";}
	
	$html.="TYPE='application/x-shockwave-flash' PLUGINSPAGE='http://www.macromedia.com/go/getflashplayer'>\n</EMBED>\n</OBJECT>";
	
	return $html;
	
}

?>
